var groupid=window.location.href.split("=")[1];
Ext.require([
    'Ext.form.Panel',
    'Ext.form.FieldSet',
    'Ext.field.Text',
    'Ext.field.Password',
    'Ext.field.Email',
    'Ext.field.Url',
    'Ext.field.Checkbox',
    'Ext.field.Spinner',
    'Ext.field.Select',
    'Ext.field.Hidden',
    'Ext.field.DatePicker',
    'Ext.field.TextArea',
    'Ext.Button',
    'Ext.data.Store'
]);

Ext.define('User', {
    extend:'Ext.data.Model',

    config:{
        fields:[
            {name:'name', type:'string'},
            {name:'password', type:'string'},
            {name:'disabled', type:'string'},
            {name:'email', type:'string'},
            {name:'bio', type:'string'},
            {name:'url', type:'string'},
            {name:'date', type:'date'},
            {name:'number', type:'integer'},
            {name:'height', type:'integer'},
            {name:'enable', type:'integer'},
            {name:'spinner', type:'integer'},
            {name:'rank', type:'string'},
            {name:'enable', type:'boolean'},
            {name:'cool', type:'boolean'},
            {name:'color', type:'string'},
            {name:'team', type:'string'},
            {name:'secret', type:'boolean'}
        ]
    }
});

Ext.define('Ranks', {
    extend:'Ext.data.Model',

    config:{
        fields:[
            {name:'rank', type:'string'},
            {name:'title', type:'string'}
        ]
    }
});

Ext.setup({
    icon:'icon.png',
    tabletStartupScreen:'tablet_startup.png',
    phoneStartupScreen:'phone_startup.png',
    glossOnIcon:false,
    onReady:function () {
        var form;
        var ranksStore = Ext.create('Ext.data.Store', {
//            data:[
//                { rank:'master', title:'Master'},
//                { rank:'padawan', title:'Student'},
//                { rank:'teacher', title:'Instructor'},
//                { rank:'aid', title:'Assistant'}
//            ],
        	proxy:{
                type:'ajax',
                url:'../companycomminutes/getgroupsaction.action?groupid='+groupid,
            },
            model:'Ranks',
            autoLoad:true,
            autoDestroy:true
        });

        var formBase = {
            url:'postUser.php',
            standardSubmit:false,
            items:[
                {
                    xtype:'fieldset',
                    title:'人员详细信息',
                    instructions:'Please enter the information above.',
                    defaults:{
                        required:true,
                        labelAlign:'left',
                        labelWidth:'40%'
                    },
                    items:[
                        {
                            xtype:'textfield',
                            name:'name',
                            label:'姓名',
                            clearIcon:true,
                            autoCapitalize:false
                        },
                        {
                            xtype:'passwordfield',
                            name:'password',
                            label:'密码',
                            clearIcon:false
                        },
                        {
                            xtype:'emailfield',
                            name:'email',
                            label:'邮箱',
                            placeHolder:'you@sencha.com',
                            clearIcon:true
                        },
                        {
                            xtype:'urlfield',
                            name:'url',
                            label:'主页',
                            placeHolder:'http://sencha.com',
                            clearIcon:true
                        },
                        {
                            xtype:'textfield',
                            name:'mobilephone',
                            label:'移动电话',
                            value:''
                        },
                        {
                            xtype:'selectfield',
                            name:'rank',
                            label:'所属部门',
                            valueField:'rank',
                            displayField:'title',
                            store:ranksStore
                        },
                        {
                            xtype:'datepickerfield',
                            name:'birthday',
                            label:'出生日期',
                            value:new Date(),
                            picker:{
                                yearFrom:1980
                            }
                        },
                        {
                            xtype:'hiddenfield',
                            name:'secret',
                            value:'false'
                        },
                        {
                            xtype:'textareafield',
                            name:'bio',
                            label:'简介',
                            maxLength:60,
                            maxRows:10
                        }
                    ]
                },

                {
                    xtype:'toolbar',
                    docked:'bottom',
                    items:[
                        {
                            text:'返回',
                            ui:'back',
                            handler:function () {
                            	window.location.href='../companyusers/companygroup.jsp?groupid='+groupid;

                            }
                        },
                        {xtype:'spacer'},
//                        {
//                            text:'Reset',
//                            handler:function () {
//                                form.reset();
//                            }
//                        },
                        {
                            text:'保存',
                            ui:'confirm',
                            handler:function () {
                                if (formBase.user) {
                                    form.updateRecord(formBase.user, true);
                                }
                                form.submit({
                                    waitMsg:{message:'Submitting'}
                                });
                            }
                        }
                    ]
                }
            ],

            listeners:{
                submit:function (form, result) {
                    console.log('success', Ext.toArray(arguments));
                },
                exception:function (form, result) {
                    console.log('failure', Ext.toArray(arguments));
                }
            }
        };
        console.log("device==="+Ext.os.deviceType);
        if (Ext.os.deviceType == 'Phone' || Ext.os.deviceType=='Tablet') {
            Ext.apply(formBase, {
                xtype:'formpanel',
                autoRender:true,
                fullscreen:true
            });

            form = Ext.Viewport.add(formBase);
        } else {
            Ext.apply(formBase, {
                autoRender:true,
                modal:true,
                hideOnMaskTap:false,
                height:505,
                width:480,
                centered:true,
                fullscreen:true
            });

            form = Ext.create('Ext.form.Panel', formBase);
            form.show();
        }
    }
});
